class Node { //节点
    constructor(element) {
        this.element = element;
        this.next = null;
    }
}
class LinkedList {
    constructor() {
        this.head = null;
        this.count = 0; //节点个数
    }
    push(element) { //向链表结尾添加节点
        let node = new Node(element);//创建节点
        if(this.head===null){//链表头为空
            this.head=node;

        }else{
            let current=this.head;//指针
            while (current.next!==null) {
                current=current.next;
            }
            current.next=node;
        }
        this.count++;//节点个数+1
    }
    getElementAt(index){//返回链表中下标指向的值
        if(index>=0&&index<this.count){
            let current = this.head;
            for(let i=0;i<index;i++){
                current = current.next
            }
            return current
        }
        return undefined
    }
    removeAt(index){//删除指定下标的元素
        if(index>=0&&index<this.count){
            let current = this.head;
            if(index===0){
                this.head = current.next
            }else{
                let previous = this.getElementAt(index-1)
                current = previous.next;
                previous.next = current.next
            }
            this.count--;
            return current.element
        }
        return undefined
    }
    insert(element,index){//向链表的特定位置插⼊⼀个新元素。
        if(index>=0&&index<this.count){
            let node = new Node(element);
            if(index=0){
                let current = this.head;
                node.next = current;
                this.head = node
            }else{
                const previous = this.getElementAt(index-1);
                const current = previous.next;
                node.next = current;
                previous.next = node
            }
            this.count++;
            return true
        }
        return false
    }
    indexOf(element){//返回元素在链表中的下标
        let current = this.head;
        for(let i=0;i<this.count&&current!=null;i++){
            if(element === current.element){
                return i
            }
            current = current.next
        }
        return -1
    }
    remove(element){//从链表中移除⼀个元素。
        let index = this.indexOf(element);
        return removeAt(index)
    }
     size(){//长度 即元素节点的个数
        return this.count;
    }
    
    getHead(){//首个节点
        return this.head
    }
}
let l = new LinkedList()
l.push("a")
l.getElementAt("a")
l.insert("b")
l.removeAt("b");
console.log(l)